Bugfix: Implement tolerant JSON decoding#1553
Merged
Merged
Conversation
…add corresponding tests
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1553 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 14 14
Lines 1325 1334 +9
Branches 142 142
=========================================
+ Hits 1325 1334 +9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the robustness of response handling in the BSBLAN client by making JSON decoding tolerant to devices that return Latin-1-encoded JSON without declaring a charset, and extends test coverage to validate the behavior.
Changes:
- Added
_Transport._read_json()to decode response bodies as UTF-8 with a Latin-1 fallback, then parse JSON. - Switched
request_with_retry()to use_read_json()instead ofaiohttp’sresponse.json(). - Added encoding-focused tests and updated the invalid-JSON edge case to align with the new decoding path.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/bsblan/_transport.py |
Introduces tolerant JSON decoding and routes all request decoding through it. |
tests/test_response_encoding.py |
Adds tests validating UTF-8 and Latin-1 response decoding. |
tests/test_bsblan_edge_cases.py |
Updates the invalid JSON test to mock read() and assert the wrapped error message. |
Comment on lines
+167
to
+171
| try: | ||
| text = raw.decode("utf-8") | ||
| except UnicodeDecodeError: | ||
| text = raw.decode("latin-1") | ||
| return cast("dict[str, Any]", json.loads(text)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This pull request improves the robustness of the BSB-LAN integration by making JSON response decoding tolerant to non-UTF-8 encodings, specifically handling Latin-1 encoded responses. It introduces a custom method for decoding JSON, updates error handling, and adds new tests to ensure correct behavior for both UTF-8 and Latin-1 encoded responses.
Response decoding improvements:
_read_jsontosrc/bsblan/_transport.pyto decode JSON responses, first attempting UTF-8 and falling back to Latin-1 if necessary, addressing issues with BSB-LAN firmwares that serve Latin-1 encoded responses without a charset declaration. [1] [2]request_with_retrymethod to use the new_read_jsonmethod, ensuring all responses are decoded using the tolerant logic.Testing enhancements:
tests/test_response_encoding.pywith tests verifying that Latin-1 and UTF-8 encoded responses are both decoded correctly.tests/test_bsblan_edge_cases.pyto mock the new decoding logic and check for the updated error message when invalid JSON is encountered.